package com.purengage.ipilih.lib;

import com.purengage.ipilih.R;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class DialogBox extends AlertDialog {
	
	ContextThemeWrapper ctw = new ContextThemeWrapper(getContext(),
			R.style.ThemeDialog);
	AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
	LayoutInflater inflater = getLayoutInflater();

	public DialogBox(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void PoupupDialog(String title, String message) {
		View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText(title);
		builder.setCustomTitle(view).setMessage(message)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					@TargetApi(11)
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).create().show();
		
	}

	public void Pilihan2Dialog(String Title, String R1, String R2) {
		final View formElementView = inflater.inflate(R.layout.popup_pilihan,
				null);
		AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
		View view = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) view.findViewById(R.id.text_title);
		tx.setText(Title);
		final RadioGroup pilihan = (RadioGroup) formElementView
				.findViewById(R.id.RadioGroup);
		RadioButton r1 = (RadioButton) formElementView
				.findViewById(R.id.pilih1);
		RadioButton r2 = (RadioButton) formElementView
				.findViewById(R.id.pilih2);
		r1.setText(R1);
		r2.setText(R2);
		builder.setView(formElementView).setCustomTitle(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						int selectId = pilihan.getCheckedRadioButtonId();

						RadioButton selectedRadio = (RadioButton) formElementView
								.findViewById(selectId);

						Toast.makeText(getContext(), "You've Selected :"
								+ selectedRadio.getText().toString(),
								Toast.LENGTH_SHORT).show();

						dialog.cancel();
					}
				}).create().show();
	}
	
	public String textFieldPopup(String Title){
		final View formElementView = inflater.inflate(R.layout.popup_textfield,
				null, false);
		View vi=inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) vi.findViewById(R.id.text_title);
		tx.setText(Title);
		
		final EditText TextField = (EditText) formElementView
				.findViewById(R.id.popupText);
		
		builder.setView(formElementView)
				.setCustomTitle(vi)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(getContext(), "You've Filled Text With :"
								+ TextField.getText().toString(),
								Toast.LENGTH_SHORT).show();
					}
				}).create().show();
		return TextField.getText().toString();
	}
	
	public void DatePopup(String Title){
		AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
		View vi=inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) vi.findViewById(R.id.text_title);
		tx.setText(Title);
		View view = inflater.inflate(R.layout.popup_datepicker, null, false);
		final DatePicker myDatePicker = (DatePicker) view
				.findViewById(R.id.myDatePicker);
		builder.setView(view)
				.setCustomTitle(vi)
				.setPositiveButton("Go", new DialogInterface.OnClickListener() {
					@TargetApi(11)
					public void onClick(DialogInterface dialog, int id) {
						int month = myDatePicker.getMonth() + 1;
						int day = myDatePicker.getDayOfMonth();
						int year = myDatePicker.getYear();

						Toast.makeText(getContext(), "You've Selected :"
								+ month + "/" + day + "/" + year,
								Toast.LENGTH_SHORT).show();
						dialog.cancel();

					}
				}).create().show();

		myDatePicker.setCalendarViewShown(false);
	}

}
